package com.flying.exceptiion;

import org.junit.Test;

/**
 * @author 刘鹏飞
 * @ClassName TestDIYException
 * @Date 2024/12/18 20:05
 * @Created by 刘鹏飞
 * @Description 测试自定义异常
 */
public class TestDIYException {
    private  String name;
    int age;


    /**
     * 测试自定义异常处理:
     */
    public void setAge(int age) throws MyException {
        if (age<0){
            throw new MyException("年龄不能小于0");
        }
        this.age=age;

    }

    public void setName(String name) throws MyException {
        if (name==null){
            throw new MyException("名字不能为空");
        }

    }

    @Test
    public void test1()  {
        TestDIYException testDIYException = new TestDIYException();
        try {
            testDIYException.setAge(-1);
            testDIYException.setName(null);
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

}


class MyException extends Exception{

    //  默认构造器
    public MyException(){}


    // 带有详细信息的构造器,信息存储在message中
    public MyException(String message) {
        super(message);
    }
}